package com.atguigu.aiproject.consumer;
import com.atguigu.aiproject.config.RabbitMQConfig;
import com.atguigu.aiproject.dto.PublishPostDTO;
import com.atguigu.aiproject.dto.ReviewResultDTO;
import com.atguigu.aiproject.entity.ReviewResult;
import com.atguigu.aiproject.service.ContentReviewService;
import com.atguigu.aiproject.service.CreationService;
import com.atguigu.aiproject.vectorstore.CreationVectorStoreManager;
import com.atguigu.aiproject.service.ReviewResultService;
import com.atguigu.aiproject.utils.MappingUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 创作作品消息消费者，处理发送到creation_details_queue队列的消息
 */
@Component
public class CreationDetailsConsumer {
    private final ContentReviewService contentReviewService;
    private final ReviewResultService reviewResultService;
    private final CreationService creationService;

    private static final Logger logger = LoggerFactory.getLogger(CreationDetailsConsumer.class);
    private final CreationVectorStoreManager creationVectorStoreManager;

    public CreationDetailsConsumer(ContentReviewService contentReviewService, ReviewResultService reviewResultService, CreationService creationService, CreationVectorStoreManager creationVectorStoreManager) {
        this.contentReviewService = contentReviewService;
        this.reviewResultService = reviewResultService;
        this.creationService = creationService;
        this.creationVectorStoreManager = creationVectorStoreManager;
    }

    @RabbitListener(queues = RabbitMQConfig.CREATION_DETAILS_QUEUE_NAME) // 修改为与发送端一致的队列
    @Transactional(rollbackFor = Exception.class)
    public void handleCreationDetails(PublishPostDTO publishPostDTO) {
        try {
            logger.info("开始处理创作详情消息: {}", publishPostDTO);
            // AI内容审核
            ReviewResultDTO aiResult = contentReviewService.performContentReview(publishPostDTO);
            //映射出ReviewResult
            ReviewResult reviewResult=MappingUtils.mappingReviewResult(aiResult, publishPostDTO);
            //保存创作作品,保存成功后返回creation_id
            Integer creationId = creationService.saveCreation(publishPostDTO, aiResult.isApproved());
            //更新审核结果，将creation_id字段加入对应审核中
            reviewResult.setCreationId(creationId);
            //保存审核结果
            reviewResultService.save(reviewResult);
            if (aiResult.isApproved()) {// 审核通过
                logger.info("AI自动审核通过: {}", publishPostDTO.getTitle());
                // 同时添加到向量数据库
                creationVectorStoreManager.add(creationId);
            } else {//审核未通过
                logger.warn("AI自动审核未通过: {}，原因: {}",
                        publishPostDTO.getTitle(), aiResult.getReason());
            }

        } catch (Exception e) {
            logger.error("处理创作详情消息失败", e);
            // 处理异常情况，可能需要重试或人工审核
        }
    }
}
